package cc.wsyw126.java.nowcoder.sword_offer;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Queue;

/**
 * Created by Administrator on 2017/4/8.
 */
public class PrintFromTopToBottom {
    public ArrayList<Integer> PrintFromTopToBottom(TreeNode root) {
        ArrayList<Integer> result = new ArrayList<>();
        if (root == null) return result;
        Queue<TreeNode> integers = new LinkedList<>();

        integers.add(root);
        while (!integers.isEmpty()) {
            TreeNode pop = integers.poll();
            if (pop.left != null) {
                integers.add(pop.left);
            }
            if (pop.right != null) {
                integers.add(pop.right);
            }
            result.add(pop.val);
        }
        return result;
    }

    public static void main(String[] args) {
        PrintFromTopToBottom printFromTopToBottom = new PrintFromTopToBottom();
        TreeNode root = new TreeNode(1);
        TreeNode left = new TreeNode(2);
        TreeNode right = new TreeNode(3);
        root.left = left;
        root.right = right;
        ArrayList<Integer> integers = printFromTopToBottom.PrintFromTopToBottom(root);
        System.out.println(integers);
    }
}
